package recharge

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/dto"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/tenant/channel"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/recharge"
	channel2 "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
)

type CreateService struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateService(ctx context.Context, svcCtx *svc.ServiceContext) *CreateService {
	return &CreateService{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (s *CreateService) GetRechargeType(typeId int64) schema.RechargeType {
	return recharge.NewRechargeTypeModel(s.ctx, s.svcCtx.Db).GetById(typeId)
}

func (s *CreateService) GetTenantChannelUsdt(id int64) channel2.TenantChannelUsdt {
	res, _ := channel.NewTenantChannelUsdtModel(s.ctx, s.svcCtx.Db).GetUsdtById(id)
	return res
}

func (s *CreateService) BuildCustomerInfoByRechargeCreate(in *rpc.RechargeCreateRequest) dto.RechargeUserInfoDto {
	var res dto.RechargeUserInfoDto

	typeInfo := s.GetRechargeType(int64(in.Type))

	switch typeInfo.ID {
	case schema.InternalLocalEWallet:
		res.Name = in.UserInfo.Name
		res.Phone = in.UserInfo.Phone
	case schema.InternalLocalUsdt:
		res.UsdtAddress = in.UserInfo.UsdtAddress
		res.UsdtAmount = in.UserInfo.UsdtAmount
	default:
		res.IsNull = true
	}

	return res
}

func (s *CreateService) BuildBankUPIByRechargeCreate(in *rpc.RechargeCreateRequest) dto.RechargeBankUPIDto {
	var res dto.RechargeBankUPIDto

	typeInfo := s.GetRechargeType(int64(in.Type))

	switch typeInfo.ID {
	case schema.InternalLocalUsdt:
		tenatInfo := s.GetTenantChannelUsdt(in.GetTenantId())
		res = dto.RechargeBankUPIDto{
			DtoStr: dto.BankUPILocalUsdtDto{
				UsdtType:    tenatInfo.UsdtType,
				UsdtAddress: in.UserInfo.UsdtAddress,
			},
		}
	default:
		res = dto.RechargeBankUPIDto{
			DtoStr: dto.BankUPIDefaultDto{},
		}
	}

	return res
}
